<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        body {
            background: url(images/2.gif);
        }

        .bigbox {
            width: 1600px;
            height: 800px;
            display: flex;
            margin: auto;
            margin-top: 70px;
        }

        .bigbox .left {
            width: 1280px;
            height: 800px;
            background-color: white;
            display: flex;
            flex-wrap: wrap;
            justify-content: space-around;
        }

        .bigbox .left .span1 {
            box-sizing: border-box;
            display: inline-block;
            width: 300px;
            height: 300px;
            margin-bottom: 95px;
            text-align: center;
            margin-top: 5px;
        }

        .bigbox .left .span1 .span4 {
            position: relative;
        }

        .bigbox .left .span1 .span4>img {
            position: absolute;
            left: 0;
            width: 100%;
        }

        .bigbox .left .span1 .btn1 {
            width: 150px;
            height: 30px;
            background-color: rgb(0, 0, 0);
            border: 0;
            border-radius: 15%;
            color: rgb(255, 255, 255);
            margin-top: 10px;
        }

        .bigbox .right {
            flex: 1;
            background-color: white;
        }

        .bigbox .right .p1 {
            font-size: 20px;
            text-align: center;
            margin-top: 22px;
            font-weight: 700;
        }

        .bigbox .right .span2 {
            height: 150px;
            display: flex;
            margin-top: 10px;
        }

        .bigbox .right .span2 .span3 {
            width: 130px;
            height: 130px;
            margin-left: 10px;
            margin-right: 10px;
        }

        .bigbox .right .span2 .span3>img {
            width: 100%;
        }

        .bigbox .right .span2 .span3 .p2 {
            font-size: 16px;
            text-align: center;
        }

        .bigbox .right .span2 .btn2 {
            width: 50px;
            height: 30px;
            margin-right: 10px;
            margin-top: 60px;
            border: 0;
            background-color: white;
        }

        .bigbox .right .span2 .btn3 {
            width: 15px;
            height: 30px;
            margin-right: 10px;
            margin-top: 60px;
            border: 0;
            background-color: white;
        }

        .bigbox .right .span2 .btn4 {
            width: 80px;
            height: 30px;
            margin-right: 10px;
            margin-top: 60px;
            border: 0;
            background-color: white;
        }
    </style>
</head>

<body>
    <div class="bigbox">
        <div class="left">
        </div>
        <div class="right">
            <p class="p1">购物车(2)</p>
            <span class="span2">
                <!-- <input type="checkbox">
                <span class="span3">
                    <img src="images/G1.jpg" alt="">
                    <p class="p2">棕色外套</p>
                </span>
                <button class="btn2">$1526</button>
                <button class="btn3">1</button>
                <button class="btn4">删除收藏</button> -->
            </span>
            <!-- <span class="span2">
                <input type="checkbox">
                <span class="span3">
                    <img src="images/G2.jpg" alt="">
                    <p class="p2">垃圾桶</p>
                </span>
                <button class="btn2">$56</button>
                <button class="btn3">1</button>
                <button class="btn4">删除收藏</button>
            </span> -->
        </div>
    </div>
</body>

</html>
<script src="./01.js"></script>
<script>
    //商品数据渲染
    var leftEle = document.querySelector('.left')
    console.log(leftEle);
    var str = ``
    for (var i = 0; i < list.length; i++) {
        console.log(list[i]);
        str += `
        <div class="span1">
            <img src = "${list[i].picture}" alt="">
            <h4>${list[i].name}</h4>
            <h4>${list[i].price}</h4>
            <button class="btn1">${list[i].btn}</button>
        </div>
        `
    }
    leftEle.innerHTML = str

    //购物车数据
    var oo = { goodlist: [] }

    function renderGouwuche() {
        var gouwuche = document.querySelector('.right');
        gouwuche.innerHTML = oo.goodlist.map((item) => {
            return  `<span class="span2">
                    <input type="checkbox">
                    <span class="span3">
                    <img src="${list[i].picture}" alt="">
                    <p class="p2">${list[i].name}</p>
                    </span>
                    <button class="btn2">${list[i].price}</button>
                    <button class="btn3">1</button>
                    <button class="btn4">删除收藏</button>
                    </span>`
        }).join('')
    }

    render();

    // 给left绑定点击事件
    var leftEle = document.querySelector('.left');
    leftEle.onclick = function (e) {
        if (e.target.tagName == 'button') {
            //获取点击商品的下标
            var btn1 = [...document.querySelectorAll('.btn1')]
            var index = btn1.findIndex((item) => {
                return item == e.target;
            })

            //添加商品数据到 购物车数组
            oo.goodlist.push(list[index]);
            //ooo.list = [ goodlist[index] ]
            //重新渲染购物车列表
            renderGouwuche();
        }
    }
</script>